package com.hgkj.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.hgkj.common.async.AsyncManager;
import com.hgkj.common.core.constant.DictConstant;
import com.hgkj.common.core.domain.ApiResult;
import com.hgkj.system.domain.dto.request.LoginRequest;
import com.hgkj.system.domain.dto.response.UserInfoResponse;
import com.hgkj.system.domain.vo.RouterVo;
import com.hgkj.system.auth.SysLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Currency;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登陆验证
 *
 * @author hgkj
 */
@Validated
@RestController
@RequestMapping("/auth")
@Slf4j
public class SysLoginController {

    @Autowired
    private SysLoginService sysLoginService;


    /**
     * 登陆接口
     * @param request 登陆信息
     * @return token信息
     */
    @PostMapping("/login")
    public ApiResult login(@Valid @RequestBody LoginRequest request) {
        sysLoginService.login(request.getUserName(), request.getPassword());
        Map<String,String> token = new HashMap<>();
        token.put(DictConstant.TOKEN_NAME, StpUtil.getTokenValue());
        return ApiResult.success(token);
    }

    /**
     * 1.获取个人信息
     * 2.获取本人角色信息
     * 3.获取本人权限信息
     */
    @GetMapping("/info")
    public ApiResult getInfo() {
        UserInfoResponse userInfo = sysLoginService.getUserInfoByUserId();
        Map<String,Object> result = new HashMap<>();
        result.put("permissions", StpUtil.getPermissionList());
        result.put("userInfo", userInfo);
        result.put("realName",userInfo.getUserName());
        result.put("roles", StpUtil.getRoleList());
        result.put("username", userInfo.getUserName());
        return ApiResult.success(result);
    }

    /**
     * 返回当前用户所需权限码
     * @return list
     */
    @GetMapping("/codes")
    public ApiResult getPermissionCodes(){
        return ApiResult.success(StpUtil.getPermissionList());
    }

    /**
     * 获取路由信息
     * @return 路由信息
     */
    @GetMapping("/getRouters")
    public ApiResult getRouters() {
        List<RouterVo> routers = sysLoginService.getRouters();
        return ApiResult.success(routers);
    }

    /**
     * 下线
     */
    @GetMapping("/logout")
    public ApiResult logout() {
        StpUtil.logout();
        return ApiResult.success("下线成功");
    }

}

